/*================ util.c ======================*/ 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* create a permanent string */
int create_string(char ** string){
	*string = (char *) calloc( 256, sizeof(char) );
	return 256;
}


/* make grow a permanent string */
int grow_string(char ** string){
	char * old = *string;
	
	int old_size = strlen(old);
	int new_size = old_size * 2;
	
	*string  = (char *) calloc(new_size, sizeof(char));
	strcpy ( *string, old );
	
	free(old);
	return new_size;
}


/* read a compleat file */
char * readFile(const char * fileName){
	FILE * pFile;
	int c, i=0, max_string_size = 0;
	char *string;
	pFile=fopen (fileName,"r");
	if (pFile==NULL){
		perror ("Error opening file");
		return NULL;
	}
	max_string_size = create_string(&string);
	while ( (c = getc (pFile)) != EOF) {
    	if (i >= max_string_size-1){
			max_string_size = grow_string(&string);
		}
		
		string[i] = c;
		++i;
	}
	
	string[i] = '\0';
	fclose (pFile);
	return string;  
}
	
